package com.example.wms;

import android.content.Context;
import android.content.Intent;

import java.util.Date;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.content.Intent;
import android.widget.EditText;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import com.example.classes.*;


public class AddItemActivity extends Activity{

    Date date;
    boolean state;
    Member owner;
    Location location;
    public String category="Misc";
    DataBase db = new DataBase(this);

    public void onCreate(Bundle savedInstanceState)
        {
            Log.v("AddItemActivity", "Made an AddItemActivity");
            db=new DataBase(this);
            db.open();
            Intent in=getIntent();
            String user=(in.getStringExtra("username"));
            owner=db.getEntry(user);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.add_item);
            date = new Date();
            state = false;

/**            LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            LocationManagerHelper lmh = new LocationManagerHelper();

            String prov;
            Criteria cr = new Criteria();

            cr.setAccuracy(Criteria.ACCURACY_COARSE);

            prov = lm.getBestProvider(cr, true);

            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10000, lmh);
            location = lm.getLastKnownLocation(String.valueOf(lm));
            lm.removeUpdates(lmh);
 */
            location = null;
            //finish();
        }

    public void tryAdd(View view)
    {
        //Intent intent= new Intent(); //(this, AddItemHandler.class);

        EditText  itemName = (EditText)findViewById(R.id.iName);
        String  name = itemName.getText().toString();
        EditText  itemDesc = (EditText)findViewById(R.id.iDescription);
        String  desc = itemDesc.getText().toString();
        EditText  itemReward = (EditText)findViewById(R.id.iReward);
        double   reward = Double.parseDouble(itemReward.getText().toString());
        EditText  itemImageLoc = null;//(EditText)findViewById(R.id.imageLoc);
        String  imageLoc = null;//itemImageLoc.getText().toString();

        Log.v("AddItemActivity", "Got the input.");

        Item i = new Item(name, desc, category, location, state, reward, date, imageLoc, owner);
        owner.addItem(i);
        Log.v("AddItemActivity", "#########         KAPPA");
        db.update(owner);
        db.close();
        Log.v("AddItemActivity", "#########         LAPPA");
        Intent gotoItemListView=new Intent(this,itemListViewActivity.class);
        gotoItemListView.putExtra("username",owner.getName());
        startActivity(gotoItemListView);
        this.finish();
        Log.v("AddItemActivity", "Made a new Item.");
        //intent.putExtra("Item", i);

        //startActivity(intent);
    }

    public void goBack(View view)
    {
        finish();
    }

    public void SetCategory(View view)
    {
        switch(view.getId())
        {
            case R.id.Heirlooms:
                category="Heirloom";
                break;
            case R.id.Keepsakes:
                category="Keepsake";
                break;
            default:
                category="Misc";
        }
    }

    public class LocationManagerHelper implements LocationListener {

        private double latitude;
        private double longitude;

        @Override
        public void onLocationChanged(Location loc) {
            latitude = loc.getLatitude();
            longitude = loc.getLongitude();
        }

        @Override
        public void onProviderDisabled(String provider) { }

        @Override
        public void onProviderEnabled(String provider) { }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        public double getLatitude() {
            return latitude;
        }

        public double getLongitude() {
            return longitude;
        }

    }
}